package com.lframework.xingyun.basedata.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.basedata.bo.product.unit.GetProductAssistantUnitBo;
import com.lframework.xingyun.basedata.bo.product.unit.QueryProductAssistantUnitBo;
import com.lframework.xingyun.basedata.entity.ProductAssistantUnit;
import com.lframework.xingyun.basedata.service.product.ProductAssistantUnitService;
import com.lframework.xingyun.basedata.vo.product.unit.ProductAssistantUnitVo;
import com.lframework.xingyun.basedata.vo.product.unit.QueryProductAssistantUnitVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品辅助单位表 Controller
 *
 */
@Api(tags = "商品辅助单位表")
@Validated
@RestController
@RequestMapping("/basedata/product/assistant/unit")
public class ProductAssistantUnitController extends DefaultBaseController {

    @Autowired
    private ProductAssistantUnitService productAssistantUnitService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"base-data:product:assistance:unit:query"})
    @GetMapping("/query")
    public InvokeResult<List<QueryProductAssistantUnitBo>> query(@Valid QueryProductAssistantUnitVo vo) {

        List<ProductAssistantUnit> datas = productAssistantUnitService.query(vo);

        List<QueryProductAssistantUnitBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryProductAssistantUnitBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(results);
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"base-data:product:assistance:unit:query"})
    @GetMapping
    public InvokeResult<GetProductAssistantUnitBo> get(@NotBlank(message = "id不能为空！") String id) {

        ProductAssistantUnit data = productAssistantUnitService.findById(id);
        if (data == null) {
            throw new DefaultClientException("商品辅助单位表不存在！");
        }

        GetProductAssistantUnitBo result = new GetProductAssistantUnitBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"base-data:product:assistance:unit:add"})
    @PostMapping("{productId}")
    public InvokeResult<Void> save(@PathVariable String productId, @Valid @RequestBody List<ProductAssistantUnitVo> vo) {

        productAssistantUnitService.save(productId, vo);

        return InvokeResultBuilder.success();
    }
}

